package com.itheima.consumer.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DirectConfiguration {

    //声明交换机方式一  方式二为直接在选择监听队列时使用注解进行创建
    @Bean
    public DirectExchange errorExchange() {
        return new DirectExchange("error.direct");
    }

    //声明队列
    @Bean
    public Queue errorQueue() {
        return new Queue("error.queue");
    }

    //声明绑定
    @Bean
    public Binding errorBinding(@Qualifier("errorQueue") Queue errorqueue, DirectExchange errorExchange) {
        return BindingBuilder.bind(errorqueue).to(errorExchange).with("error");

    }

    @Bean
    public MessageRecoverer messageRecover(RabbitTemplate rabbitTemplate){
        return new RepublishMessageRecoverer(rabbitTemplate, "error.direct", "error");
    }

}
